package com.nearby.demand.ui.auto;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nearby.demand.R;

import java.util.Random;


public class DemoActivity extends FragmentActivity {
    AutoScrollViewPager viewPager;
    int[] colours = new int[]{Color.CYAN, Color.BLUE, Color.MAGENTA};

    private class TestAdapter extends FragmentPagerAdapter {

        public TestAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return colours.length;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new ColourFragment();
            Bundle args = new Bundle();
            args.putInt("colour", colours[position]);
            args.putInt("identifier", position);
            args.putInt("Values", Values);
            fragment.setArguments(args);
            return fragment;
        }

    }

    int Values = 0;
    SimpleDraweeView sdview_bg;
    String url[] = new String[]{
            "/images/index/77f50e3b31594133b0d653dd46fb9931.jpeg",
            "/images/index/fb9af9f9b9f544a0b7dd6f304c125473.jpeg",
            "/images/index/6af8e7b5628e4074a285b8246b0b2441.jpeg",
            "/images/index/704fa6a439614c0f9ecbe788d6a019b7.jpeg",
            "/images/index/5594efa2735e46bdb3c529ef364d5389.jpeg",
            "/images/index/99fa28d9c00044a2b56026be46928eba.jpeg",
            "/images/index/60eed74db7e440bb88b770b55a8ee3fd.jpeg",
            "/images/index/fa8ef1710e104c89941b3e7eb8e04729.jpeg",
            "/images/index/13b62ef2ad6d4f839b5f139cece7b21b.jpeg",
            "/images/index/28dd91f266194fbd87498dac382778a9.jpeg",
            "/images/index/8bcb061608c746ed9ffb9e820b16e7db.jpeg"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auto_main);
        TextView tx_show = (TextView) findViewById(R.id.tx_show);
        sdview_bg = (SimpleDraweeView) findViewById(R.id.sdview_bg);
        DraweeController controller3 = Fresco.newDraweeControllerBuilder()
                .setUri("http://192.168.2.153:8080/MccServer"+url[new Random().nextInt(11)])
                .build();
        sdview_bg.setController(controller3);
        Values = new Random().nextInt(2);
        if (Values > 0) {
            tx_show.setVisibility(View.GONE);
        } else {
            tx_show.setVisibility(View.VISIBLE);
        }
        viewPager = (AutoScrollViewPager) findViewById(R.id.vp);
        viewPager.setInterval(3000);
        viewPager.setAdapter(getSupportFragmentManager(), new TestAdapter(getSupportFragmentManager()));
        //这个OnPageChangeListener是必须的
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                System.out.println("onpageSelected " + i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //        viewPager.startAutoLoop();
    }

    @Override
    protected void onPause() {
        super.onPause();
        viewPager.stopAutoLoop();
    }
}